package com.xia.handle;

import com.xia.exception.MyException;
import com.xia.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class DefaultExceptionHandler {
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof MyException) {
            MyException bootException = (MyException) e;
            return Result.build(bootException.getCode(), bootException.getMessage());
        }
        LOGGER.error("【系统异常】：{}", e);
        return Result.build(500, "未知错误");
    }
}
